/**
 * 
 */
package com.ac4j.sample;

/**
 * @author Administrator
 * 
 */
public class SampleImage {
	Byte[][] m_value;
	int m_width;
	int m_height;

	/**
	 * @return the m_value
	 */
	public Byte[][] getValue() {
		return m_value;
	}

	/**
	 * @param m_value
	 *            the m_value to set
	 */
	public void setValue(Byte[][] m_value, int width, int height) {
		this.m_value = m_value;
		m_width = width;
		m_height = height;
	}

	/**
	 * @return the m_width
	 */
	public int getWidth() {
		return m_width;
	}

	/**
	 * @return the m_height
	 */
	public int getHeight() {
		return m_height;
	}

	/**
	 * Creates a new <code>SampleImage</code> with blank value, which size is
	 * determained by width*height
	 * 
	 * @param width
	 * @param height
	 */
	public SampleImage(int width, int height) {
		this.m_value = new Byte[height][width];
		this.m_width = width;
		this.m_height = height;
	}

	/**
	 * Creates a new <code>SampleImage</code> refered to an exist 2D
	 * <code>Byte</code> array.
	 * 
	 * @param value
	 *            the Byte Array in 2-dimension
	 * @param width
	 *            Up bound of the Byte[width][]
	 * @param height
	 *            Up bound of the Byte[][height]
	 */
	public SampleImage(Byte[][] value, int width, int height) {
		this.m_value = value;
		this.m_width = width;
		this.m_height = height;
	}

	/**
	 * Returns a new <code>SampleImage</code> containing the required area
	 * 
	 * @param startX
	 *            X of the choping area(start with 0)
	 * @param startY
	 *            Y of the choping area(start with 0)
	 * @param width
	 *            Width of the choping area
	 * @param height
	 *            Height of the choping area
	 * 
	 * @return a <code>SampleImage</code> that is referred to the required area
	 * @throws <code>IndexOutOfBoundsException</code> if the argument is out of
	 *         bounds.
	 * 
	 */
	public SampleImage Chop(int startX, int startY, int width, int height) {

		if (startX < 0 || startY < 0 || width <= 0 || height <= 0
				|| (startX + height) > this.getHeight()
				|| (startY + width) > this.getWidth())
			throw new IndexOutOfBoundsException(
					"Arguments of chop are out of bounds");
		SampleImage snew = new SampleImage(width, height);
		for (int i = 0; i < height; i++)
			for (int j = 0; j < width; j++)
				snew.getValue()[i][j] = this.m_value[startX + i][startY + j];
		return snew;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		String s = "";
		for (int i = 0; i < getHeight(); i++) {
			for (int j = 0; j < getWidth(); j++) {
				if (m_value[i][j] == 1)
					s = s + "*";
				else
					s = s + " ";
			}
			s = s + "\n";
		}
		return s;
	}
}
